package handler

import (
	"fmt"
	"github.com/antchfx/htmlquery"
	"github.com/gocolly/colly"
	"log"
	"sbd_fldj/models"
	"sbd_fldj/utils"
	"strconv"
	"strings"
)

func ReListPageSizeHandler() (pageSize int) {
	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=4F588DC05BDBF3D2C3C5F3873862B72D; wdcid=3324e83c4b4833d9; _gscu_1942319172=62363514zs207j20; _gscbrs_1942319172=1; wdses=11eab50dfb9f14b8; _gscs_1942319172=t624320311h9mmx20|pv:10; wdlast=1662433084")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		// fmt.Println(htmlquery.OutputHTML(doc, false))
		node := htmlquery.FindOne(doc, `/html/body/div[4]/div/div[3]/span`)
		str := htmlquery.OutputHTML(node, false)
		index := strings.LastIndex(str, "/")
		pageSize, err = strconv.Atoi(str[index+1 : len(str)-3])
		if err != nil {
			fmt.Println("strconv.Atoi err")
		}

	})
	c.Visit("http://202.127.42.127/moafer/list.jsp?page=0")

	return
}

func ReListHandler(pageSize int) (isCompanys []models.Company, err error) {
	isCompany := models.Company{}
	pageNo := strconv.Itoa(pageSize)
	url := fmt.Sprintf("http://202.127.42.127/moafer/list.jsp?page=%s", pageNo)

	c := colly.NewCollector(colly.UserAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"))

	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "JSESSIONID=4F588DC05BDBF3D2C3C5F3873862B72D; wdcid=3324e83c4b4833d9; _gscu_1942319172=62363514zs207j20; _gscbrs_1942319172=1; wdses=11eab50dfb9f14b8; _gscs_1942319172=t624320311h9mmx20|pv:10; wdlast=1662433084")

	})
	c.OnResponse(func(r *colly.Response) {
		doc, err := htmlquery.Parse(strings.NewReader(string(r.Body)))
		if err != nil {
			log.Fatal(err)
		}
		nodes := htmlquery.Find(doc, `/html/body/div[4]/div/div[2]/table/tbody/tr`)
		for key, node := range nodes {
			if key == 0 {
				continue
			}
			// fmt.Println(htmlquery.OutputHTML(node, false))
			// vive 标识符
			str := htmlquery.FindOne(node, "./td[1]/a")
			vive := htmlquery.SelectAttr(str, "href")
			vive_index := strings.LastIndex(vive, "/")
			isCompany.Vive = vive[vive_index+1 : len(vive)-4]
			// 企业名称
			isCompany.Company = htmlquery.InnerText(str)
			// 产品通用名称
			Tymc := htmlquery.FindOne(node, "./td[2]/a")
			isCompany.Tymc = htmlquery.InnerText(Tymc)
			// 产品商品名称
			Spmc := htmlquery.FindOne(node, "./td[3]/a")
			isCompany.Spmc = htmlquery.InnerText(Spmc)

			// 产品商品名称
			Cpxt := htmlquery.FindOne(node, "./td[4]/a")
			isCompany.Cpxt = htmlquery.InnerText(Cpxt)

			// 登记技术指标
			Jszb := htmlquery.FindOne(node, "./td[5]/a")
			isCompany.Jszb = htmlquery.InnerText(Jszb)

			// 适宜范围
			Stfw := htmlquery.FindOne(node, "./td[6]/a")
			isCompany.Syfw = htmlquery.InnerText(Stfw)

			// 登记证号
			Djzh := htmlquery.FindOne(node, "./td[7]/a")
			isCompany.Djzh = htmlquery.InnerText(Djzh)

			// 登记证号
			Yxq := htmlquery.FindOne(node, "./td[8]/a")
			isCompany.Yxq = htmlquery.InnerText(Yxq)

			isCompany.ID = utils.SnowId()
			isCompanys = append(isCompanys, isCompany)
		}
	})
	c.Visit(url)
	return
}
